package ar.com.psicosoft.persistence.psicosoft;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ar.com.psicosoft.exception.PsicosoftException;
import ar.com.psicosoft.model.psicosoft.Profesional;

@Repository(value = "profesionalDao")
public class ProfesionalDaoImpl implements ProfesionalDao, Serializable {

	private static final long serialVersionUID = 1L;
	@PersistenceContext(unitName = "psicosoftPU")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	@Override
	public Profesional buscarProfesionalPorId(Integer idProfesional)
			throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("Profesional.buscarPorId");
		query.setParameter("idProfesional", idProfesional);
		try {
			List<Profesional> resultList = query.getResultList();
			if (!resultList.isEmpty()) {
				// ignores multiple results
				return resultList.get(0);
			} else {
				return null;
			}
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar profesional en la Base de Datos");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profesional> buscarProfesionales() throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("Profesional.buscarProfesionales");
		try {
			List<Profesional> resultList = query.getResultList();
			return resultList;
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar profesionales en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void guardarProfesional(Profesional profesional)
			throws PsicosoftException {
		try {
			this.entityManager.persist(profesional);
			this.entityManager.flush();
			this.entityManager.refresh(profesional);
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al insertar profesional en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void modificarProfesional(Profesional profesional)
			throws PsicosoftException {
		try {
			this.entityManager.merge(profesional);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar profesional en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void eliminarProfesional(Profesional profesional)
			throws PsicosoftException {
		try {
			this.entityManager.remove(this.entityManager.merge(profesional));
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al eliminar profesional en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void bajaProfesional(Profesional profesional)
			throws PsicosoftException {
		try {
			this.entityManager.merge(profesional);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar profesional en la Base de Datos");
		}
	}

}